package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.service.CommonService;
import com.maixy.studyonlineapi.view.MediaView;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @ClassName CommonController
 * @Description TODO
 * @Date 2020/11/28 12:34
 * @Author maixy
 */
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS}, allowCredentials = "true")
@RestController
@RequestMapping("/v1/common")
public class CommonController {

    @Resource
    private CommonService commonService;

    @PostMapping("/uploadImage")
    public ApiResponse uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (null == file) {
            throw new TipException("参数缺失");
        }
        MediaView media = commonService.uploadImage(file);
        return ApiResponse.ok()
                .addValueToData("mediaView", media);
    }

    @PostMapping("/uploadVideo")
    public ApiResponse uploadVideo(@RequestParam("file") MultipartFile file) throws IOException {
        if (null == file) {
            throw new TipException("参数缺失");
        }
        MediaView media = commonService.uploadVideo(file);
        return ApiResponse.ok()
                .addValueToData("mediaView", media);
    }

    @PostMapping("/uploadPDF")
    public ApiResponse uploadPDF(@RequestParam("file") MultipartFile file) throws IOException {
        if (null == file) {
            throw new TipException("参数缺失");
        }
        MediaView media = commonService.uploadPDF(file);
        return ApiResponse.ok()
                .addValueToData("mediaView", media);
    }

    @GetMapping("/getZip")
    public ApiResponse getZip() throws IOException {
        commonService.getZip();
        return ApiResponse.ok();
    }

    @GetMapping("/buildActivityEsData")
    public ApiResponse buildActivityEsData() {
        commonService.buildActivityEsData();
        return ApiResponse.ok();
    }


}
